package com.atjg.autocat.main

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Handler
import com.atjg.autocat.R
import com.atjg.autocat.mqtt.MqttConfig
import com.atjg.autocat.utils.CrashHandler
import com.atjg.autocat.utils.MusicPlayUtils
import com.atjg.autocat.viewModel.MainViewModel
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.Utils
import com.tencent.bugly.crashreport.CrashReport
import com.zhy.baselibrary.api.ApiFactory
import com.zhy.baselibrary.base.BaseApp
import com.zhy.baselibrary.expand.ToastMgr
import kotlinx.coroutines.Job


class App : BaseApp() {
    companion object {
        val app1 = "3"
        var baseUrl = "https://jshbhs.gyiot.cn/"//嘉森接口地址
        var fileUrl = "http://222.187.215.82:9529/"//嘉森接口地址
        var mHanler: Handler? = null
        var projectName = "嘉森"
    }

    private val model = MainViewModel()
    private var job: Job? = null
    override fun onCreate() {
        super.onCreate()
        metaData()
        mHanler = Handler()
        ToastMgr.init(this)
        ApiFactory.factory?.setTimeout(180)
        CrashReport.initCrashReport(applicationContext, "f6e77e3105", true)
        MusicPlayUtils.playBgM(this, R.raw.music_webcome)
        val mCrashHandler = CrashHandler.getInstance()
        mCrashHandler.init(applicationContext, javaClass)
        initConfig()
//        ProcessLifecycleOwner.get().lifecycle.addObserver(ApplicationObserver())
        AppUtils.registerAppStatusChangedListener(object : Utils.OnAppStatusChangedListener {
            override fun onForeground(activity: Activity?) {
                job?.cancel()
            }

            override fun onBackground(activity: Activity?) {
                startActivity(activity)
            }
        })
    }


    private fun startActivity(activity: Activity?) {
        job?.cancel()
        job = model.testTime(null, {
            val activityIntent = Intent(activity, SplashActivity::class.java)
            activity?.startActivity(activityIntent)
        }, {}, 5000)
    }

    private fun metaData() {
        val metaData: Bundle = packageManager.getApplicationInfo(
            packageName,
            PackageManager.GET_META_DATA
        ).metaData
        baseUrl = metaData.getString("apiUrl") ?: ""
        MqttConfig.TOPIC_NAME = metaData.getString("mqUser") ?: ""
        MqttConfig.TOPIC_CODE = metaData.getString("mqPWD") ?: ""
        MqttConfig.TOPIC_ADDRESS = metaData.getString("mqTopic") ?: ""
        projectName = metaData.getString("projectName") ?: ""
        ApiFactory.factory?.add(baseUrl)
    }

    @SuppressLint("AuthLeak")
    private fun initConfig() {
       val time7 =  SPUtils.getInstance().getInt("time7",0)
        if(time7 == 0){
            SPUtils.getInstance().put("time7",10)
        }
        val time8 =  SPUtils.getInstance().getInt("time8",0)
        if(time8 == 0){
            SPUtils.getInstance().put("time8",10)
        }
        val tc1Time1 = SPUtils.getInstance("tc1").getInt("time1",0)
        if(tc1Time1 == 0){
            SPUtils.getInstance("tc1").put("time1",120)
        }
        val tc1Time2 = SPUtils.getInstance("tc1").getInt("time2",0)
        if(tc1Time2 == 0){
            SPUtils.getInstance("tc1").put("time2",60)
        }
        val tc1Time3 = SPUtils.getInstance("tc1").getInt("time3",0)
        if(tc1Time3 == 0){
            SPUtils.getInstance("tc1").put("time3",120)
        }
        val tc1Time4 = SPUtils.getInstance("tc1").getInt("time4",0)
        if(tc1Time4 == 0){
            SPUtils.getInstance("tc1").put("time4",60)
        }
        val tc1Time5 = SPUtils.getInstance("tc1").getInt("time5",0)
        if(tc1Time5 == 0){
            SPUtils.getInstance("tc1").put("time5",420)
        }
        val tc1Time6 = SPUtils.getInstance("tc1").getInt("time6",0)
        if(tc1Time6 == 0){
            SPUtils.getInstance("tc1").put("time6",2820)
        }

        val tc2Time1 = SPUtils.getInstance("tc2").getInt("time1",0)
        if(tc2Time1 == 0){
            SPUtils.getInstance("tc2").put("time1",90)
        }
        val tc2Time2 = SPUtils.getInstance("tc2").getInt("time2",0)
        if(tc2Time2 == 0){
            SPUtils.getInstance("tc2").put("time2",60)
        }
        val tc2Time3 = SPUtils.getInstance("tc2").getInt("time3",0)
        if(tc2Time3 == 0){
            SPUtils.getInstance("tc2").put("time3",90)
        }
        val tc2Time4 = SPUtils.getInstance("tc2").getInt("time4",0)
        if(tc2Time4 == 0){
            SPUtils.getInstance("tc2").put("time4",60)
        }
        val tc2Time5 = SPUtils.getInstance("tc2").getInt("time5",0)
        if(tc2Time5 == 0){
            SPUtils.getInstance("tc2").put("time5",300)
        }
        val tc2Time6 = SPUtils.getInstance("tc2").getInt("time6",0)
        if(tc2Time6 == 0){
            SPUtils.getInstance("tc2").put("time6",1800)
        }
        val tc3Time1 = SPUtils.getInstance("tc3").getInt("time1",0)
        if(tc3Time1 == 0){
            SPUtils.getInstance("tc3").put("time1",90)
        }
        val tc3Time2 = SPUtils.getInstance("tc3").getInt("time2",0)
        if(tc3Time2 == 0){
            SPUtils.getInstance("tc3").put("time2",60)
        }
        val tc3Time3 = SPUtils.getInstance("tc3").getInt("time3",0)
        if(tc3Time3 == 0){
            SPUtils.getInstance("tc3").put("time3",90)
        }
        val tc3Time4 = SPUtils.getInstance("tc3").getInt("time4",0)
        if(tc3Time4 == 0){
            SPUtils.getInstance("tc3").put("time4",60)
        }
        val tc3Time5 = SPUtils.getInstance("tc3").getInt("time5",0)
        if(tc3Time5 == 0){
            SPUtils.getInstance("tc3").put("time5",300)
        }
        val tc3Time6 = SPUtils.getInstance("tc3").getInt("time6",0)
        if(tc3Time6 == 0){
            SPUtils.getInstance("tc3").put("time6",1200)
        }

//        SPUtils.getInstance("tc1").put("time1",30)
//        SPUtils.getInstance("tc1").put("time2",15)
//        SPUtils.getInstance("tc1").put("time3",30)
//        SPUtils.getInstance("tc1").put("time4",15)
//        SPUtils.getInstance("tc1").put("time5",60)
//        SPUtils.getInstance("tc1").put("time6",60)
//        SPUtils.getInstance("tc1").put("time7",15)
//        SPUtils.getInstance("tc1").put("time8",15)
    }
}